package com.kanshu.ksgb.fastread.doudou.module.reader.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.share.ShareBean;
import com.kanshu.ksgb.fastread.doudou.common.share.ShareUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.module.book.presenter.BookListPresenter;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.requestparams.ChapterRequestParams;

import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 阅读器分享对话框
 */
public class ReaderShareDialog extends Dialog {
    private Activity mActivity;
    private ShareBean mShareBean;
    private static int flag = 0;

    public ReaderShareDialog(@NonNull Context context, ShareBean bean) {
        super(context, R.style.dialog_style);
        mActivity = (Activity) context;
        mShareBean = bean;
        init();
    }
    public static void show(Activity activity, String bookId, String contentId) {
        if(flag == 0) {
            flag = 1;
            BookListPresenter presenter = new BookListPresenter(null);
            ChapterRequestParams params = new ChapterRequestParams();
            params.book_id = bookId;
            params.content_id = contentId;
            presenter.getChapterShareInfo(params, new INetCommCallback<ShareBean>() {
                @Override
                public void onResponse(ShareBean shareBean) {
                    show(activity, shareBean);
                }

                @Override
                public void onError(int code, String errDesc) {
                    flag = 0;
                    show(activity, null);
                }
            });
        }
    }


    public static ReaderShareDialog show(Activity activity, final ShareBean bean) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        ReaderShareDialog dlg = new ReaderShareDialog(activity, bean);
        dlg.show();
        WindowManager.LayoutParams layoutParams = dlg.getWindow().getAttributes();
        dlg.getWindow().setWindowAnimations(R.style.AnimBottom);
        layoutParams.width = DisplayUtils.getScreenWidth(activity);
        layoutParams.gravity = Gravity.BOTTOM;
        dlg.getWindow().setAttributes(layoutParams);
        return dlg;
    }

    private void init() {
        setContentView(R.layout.dialog_reader_share_layout);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.wechat, R.id.wechat_circle, R.id.qq, R.id.cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wechat:
                if (mShareBean == null) {
                    return;
                }
                mShareBean.platform = Wechat.NAME;
                ShareUtils.share(mActivity, mShareBean, this);
                dismiss();
                break;
            case R.id.wechat_circle:
                if (mShareBean == null) {
                    return;
                }
                mShareBean.platform = WechatMoments.NAME;
                ShareUtils.share(mActivity, mShareBean, this);
                dismiss();
                break;
            case R.id.qq:
                if (mShareBean == null) {
                    return;
                }
                mShareBean.platform = QQ.NAME;
                ShareUtils.share(mActivity, mShareBean, this);
                dismiss();
                break;
            case R.id.cancel:
                dismiss();
                break;
        }
    }
    @Override
    public void dismiss() {
        if (mActivity != null && !mActivity.isFinishing()) {
            flag = 0;
            super.dismiss();
        }
    }
}
